// 判断需要传入的表单参数是否为空
export function testParams(obj, context = this.form) {
  let message;
  let paramsComplete = true;
  for (const prop in obj) {
    prop.split(/\s|\||-/).forEach(item => {
      if (!context[item]) {
        paramsComplete = false;
        message = obj[prop];
      }
    });
    if (!paramsComplete) break;
  }
  if (!paramsComplete) {
    this.$message({
      type: "warning",
      message: message
    });
  }
  return paramsComplete;
}

// 添加时分秒
export function addHours(str, configuration = "start") {
  return (
    (str || "") + " " + (configuration !== "start" ? "23:59:59" : "00:00:00")
  );
}
// 去除时分秒
export function removeHours(str) {
  const TIMEREG = /(\d{4}\W+\d{1,2}\W+\d{1,2})(?:\s+[^\s]+)+/;
  const matched = str.match(TIMEREG) && str.match(TIMEREG)[1];
  return matched || str;
}
//当前年事件
export function getFullYear() {
  let year =  new Date().getFullYear();
  return year;
}
// 获得今年年份
export const thisYear = "" + new Date().getFullYear();

export function getYearsArr(years, val) {
  let yearsArr = [];
  if (val == 1) {
    let yearBefore = new Date().getFullYear();
    years = yearBefore - 2017 + 1;
    let yearLast = 2016;
    for (let i = 0; i < years; i++) {
      yearsArr.push({
        label: "" + ++yearLast,
        value: "" + yearLast
      });
    }
  } else {
    if (typeof years !== "number" || isNaN(years) || years <= 0) years = 2;
    let yearBefore = new Date().getFullYear() - 2;
    for (let i = 0; i < years; i++) {
      yearsArr.push({
        label: "" + ++yearBefore,
        value: "" + yearBefore
      });
    }
  }
  return yearsArr;
}
// 获取月份
export function getMonthArr(val, data) {
  let monthArr = [];
  let current = new Date().getMonth();
  let currentDate = new Date().getDate();

  for (let i = 1; i < 13; i++) {
    let disabled = false;
    // 判定当前是否大于10号23:59:59 如果是则可以在考核一个月数据
    if (data) {
      if ((i == current && currentDate < 11) || i > current) {
        disabled = true;
      }
    } else {
      if (!current && i == 12 && currentDate <= 10) {
        disabled = true;
      }
    }
    // if ((i == current && data) || (i > current && data) || (end && i == 12)) {
    //   disabled = true;
    // }
    // 判定当前是否大于10号23:59:59 如果是则可以在考核一个月数据
    // if ((i == current || (end && i == 12)) && currentDate > 10) {
    //   disabled = false;
    // }
    monthArr.push({
      label: `${i}月`,
      value: i,
      disabled: disabled
    });
  }
  return monthArr;
}

// 获取当前时间
export function getCurrentDate(format) {
  var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth();//得到月份
    var date = now.getDate();//得到日期
    var day = now.getDay();//得到周几
    var hour = now.getHours();//得到小时
    var minu = now.getMinutes();//得到分钟
    var sec = now.getSeconds();//得到秒
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var time = "";
    //精确到天
    if(format==1){
      time = year + "-" + month + "-" + date;
    }
    //精确到分
    else if(format==2){
      time = year + "-" + month + "-" + date+ " " + hour + ":" + minu + ":" + sec;
	  } else if(format==3){
      time = year + "-" + month + "-" + date+ " " + hour + ":" + minu;
    }
	  // if (time && time.indexOf("-")!==-1) time = time.replace(/\-/g, "/");//兼容苹果内核
    return time;
}
